/** 
 * 
 */
package utils;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * The class TijdsMoment1Dag provides control over time elements. Purpose is to respect opening
 * hours of the campus and blocks of 2 or 4 hours course between a given timeframe. In the default
 * constructor without parameter we impose the minimal duration of a two hours course.
 * 
 * @author Nicolas Vanderhulst
 * @version 1.0
 * @serial 2011100201
 * 
 */
public class BackupOfTijdsMoment1Dag {

	private String lesDag;
	private int startUur;
	private int startMinuten;
	private int duurTijd;
	private final int tijdstip;
	private final int weekDag;
	private final int totaleTijd;
	private int year;
	private int month;
	private int day;
	private final int duration;
	private int sDate;
	private static final int DUUR = 100;
	private static final int WEEK_START = 17;
	private static final int WEEK_END = 22;
	private static final int SAT_START = 9;
	private static final int SAT_END = 14;

	@SuppressWarnings("boxing")
	public BackupOfTijdsMoment1Dag() {

		String[] weekdays = new DateFormatSymbols().getWeekdays();
		SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
		Calendar c = GregorianCalendar.getInstance();
		c.getTime().getTime();
		c.setLenient(false);

		tijdstip = Integer.parseInt(sdf.format(c.getTime().getTime()));
		weekDag = c.get(Calendar.DAY_OF_WEEK);
		duration = DUUR * 2;
		totaleTijd = tijdstip + duration;

		try {
			switch (weekDag) {
			case 1:
				throw new IllegalArgumentException("De ingegeven datum mag niet op een zondag vallen.");
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				if ((tijdstip < WEEK_START) || (tijdstip > WEEK_END)) {
					throw new IllegalArgumentException("De lessen worden gepland op weekdagen tussen 17.00 en 22.00");
				} else {
					if (totaleTijd > WEEK_END) {
						throw new IllegalArgumentException(
								"De lessen worden gepland op weekdagen tussen 17.00 en 22.00");
					} else {
						lesDag = weekdays[(c.get(Calendar.DAY_OF_WEEK))];
						startUur = c.get(Calendar.HOUR_OF_DAY);
						startMinuten = c.get(Calendar.MINUTE);
						duurTijd = (duration / 100) * 60;
						year = c.get(Calendar.YEAR);
						month = c.get(Calendar.MONTH);
						day = c.get(Calendar.DAY_OF_MONTH);
					}
				}
				break;
			case 7:
				if ((tijdstip < SAT_START) || (tijdstip > SAT_END)) {
					throw new IllegalArgumentException("De lessen worden gepland op zaterdag tussen 9.00 en 14.00");
				} else {
					if (totaleTijd > SAT_END) {
						throw new IllegalArgumentException("De lessen worden gepland op weekdagen tussen 9.00 en 14.00");
					} else {
						lesDag = weekdays[(c.get(Calendar.DAY_OF_WEEK))];
						startUur = c.get(Calendar.HOUR_OF_DAY);
						startMinuten = c.get(Calendar.MINUTE);
						duurTijd = (duration / 100) * 60;
						year = c.get(Calendar.YEAR);
						month = c.get(Calendar.MONTH) + 1;
						day = c.get(Calendar.DAY_OF_MONTH);
					}
				}
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("boxing")
	public BackupOfTijdsMoment1Dag(int y, int m, int d, int h, int mi, int b) {

		String[] weekdays = new DateFormatSymbols().getWeekdays();
		SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
		Calendar c = GregorianCalendar.getInstance();
		c.setLenient(false);
		c.set(Calendar.YEAR, y);
		c.set(Calendar.MONTH, m - 1);
		c.set(Calendar.DAY_OF_MONTH, d);
		c.set(Calendar.HOUR_OF_DAY, h);
		c.set(Calendar.MINUTE, mi);
		c.set(Calendar.SECOND, 0);
		c.getTime().getTime();
		tijdstip = Integer.parseInt(sdf.format(c.getTime().getTime()));
		weekDag = c.get(Calendar.DAY_OF_WEEK);
		duration = DUUR * b;
		totaleTijd = tijdstip + duration;

		try {
			switch (weekDag) {
			case 1:
				throw new IllegalArgumentException("De ingegeven datum mag niet op een zondag vallen.");
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				if ((tijdstip < WEEK_START) || (tijdstip > WEEK_END)) {
					throw new IllegalArgumentException("De lessen worden gepland op weekdagen tussen 17.00 en 22.00");
				} else {
					if (totaleTijd > WEEK_END) {
						throw new IllegalArgumentException(
								"De lessen worden gepland op weekdagen tussen 17.00 en 22.00");
					} else {
						lesDag = weekdays[(c.get(Calendar.DAY_OF_WEEK))];
						startUur = c.get(Calendar.HOUR_OF_DAY);
						startMinuten = c.get(Calendar.MINUTE);
						duurTijd = (duration / 100) * 60;
						year = c.get(Calendar.YEAR);
						month = c.get(Calendar.MONTH);
						day = c.get(Calendar.DAY_OF_MONTH);
					}
				}
				break;
			case 7:
				if ((tijdstip < SAT_START) || (tijdstip > SAT_END)) {
					throw new IllegalArgumentException("De lessen worden gepland op zaterdag tussen 9.00 en 14.00");
				} else {
					if (totaleTijd > SAT_END) {
						throw new IllegalArgumentException("De lessen worden gepland op weekdagen tussen 9.00 en 14.00");
					} else {
						lesDag = weekdays[(c.get(Calendar.DAY_OF_WEEK))];
						startUur = c.get(Calendar.HOUR_OF_DAY);
						startMinuten = c.get(Calendar.MINUTE);
						duurTijd = (duration / 100) * 60;
						year = c.get(Calendar.YEAR);
						month = c.get(Calendar.MONTH) + 1;
						day = c.get(Calendar.DAY_OF_MONTH);
					}
				}
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return This method returns the lesDag, date, duurTijd in String format
	 */
	@Override
	public String toString() {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, startUur);
		cal.set(Calendar.MINUTE, startMinuten);
		Date g = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		String sDate = lesDag + " " + sdf.format(g) + " " + duurTijd;
		return sDate;
	}

	/**
	 * @return This method checks the equality between two TijdsMoment1Dag
	 */
	/*
	 * public boolean equals(Object o) { if(!(o instanceof TijdsMoment1Dag)) return false;
	 * TijdsMoment1Dag t = (TijdsMoment1Dag)o; return t.year == year && t.month == month && t.day ==
	 * day && t.startUur == startUur && t.startMinuten == startMinuten && t.duurTijd == duurTijd; }
	 */

	/**
	 * @return This method compares two TijdsMoment1Dag
	 */
	public int compareTo(Object o) {
		BackupOfTijdsMoment1Dag t = (BackupOfTijdsMoment1Dag) o;
		if (t.year < year) {
			return -1;
		} else if (t.year > year) {
			return 1;
		} else {
			if (t.month < month) {
				return -1;
			} else if (t.month > month) {
				return 1;
			} else {
				if (t.day < day) {
					return -1;
				} else if (t.day > day) {
					return 1;
				} else {
					if (t.startUur < startUur) {
						return -1;
					} else if (t.startUur > startUur) {
						return 1;
					} else {
						if (t.startMinuten < startMinuten) {
							return -1;
						} else if (t.startMinuten > startMinuten) {
							return 1;
						} else {
							return 0;
						}
					}
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + day;
		result = prime * result + duration;
		result = prime * result + duurTijd;
		result = prime * result + ((lesDag == null) ? 0 : lesDag.hashCode());
		result = prime * result + month;
		result = prime * result + sDate;
		result = prime * result + startMinuten;
		result = prime * result + startUur;
		result = prime * result + tijdstip;
		result = prime * result + totaleTijd;
		result = prime * result + weekDag;
		result = prime * result + year;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof BackupOfTijdsMoment1Dag)) {
			return false;
		}
		BackupOfTijdsMoment1Dag other = (BackupOfTijdsMoment1Dag) obj;
		if (day != other.day) {
			return false;
		}
		if (duration != other.duration) {
			return false;
		}
		if (duurTijd != other.duurTijd) {
			return false;
		}
		if (lesDag == null) {
			if (other.lesDag != null) {
				return false;
			}
		} else if (!lesDag.equals(other.lesDag)) {
			return false;
		}
		if (month != other.month) {
			return false;
		}
		if (sDate != other.sDate) {
			return false;
		}
		if (startMinuten != other.startMinuten) {
			return false;
		}
		if (startUur != other.startUur) {
			return false;
		}
		if (tijdstip != other.tijdstip) {
			return false;
		}
		if (totaleTijd != other.totaleTijd) {
			return false;
		}
		if (weekDag != other.weekDag) {
			return false;
		}
		if (year != other.year) {
			return false;
		}
		return true;
	}

	/**
	 * @return This method checks for overlappingen
	 */
	public boolean overlapping(BackupOfTijdsMoment1Dag tijdsMoment) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(false);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, startUur);
		cal.set(Calendar.MINUTE, startMinuten);
		if (tijdsMoment.sDate > sDate) {
			return true;
		} else {
			return false;
		}

	}

}
